#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QKeyEvent>
#include <QToolButton>
#include "bullet.h"
#include <QtGlobal>
#include <QTime>
#include <QDebug>

//qsrand(time(NULL));
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    this->setFixedSize(500,500);
    mGameview.setSceneRect(QRect(0,0,480,480));

    //场景视图
    mGameview.setScene(&mScene);
    //视图他爹
    mGameview.setParent(this);
    mGameview.show();
    //开始界面
    //开始场景初始化
    BeginScence.setSceneRect(QRect(0,0,480,480));
    BeginScence.addPixmap(QPixmap(":/img/cover .jpg"));
    //设置视图场景
    mGameview.setScene(&BeginScence);
    mGameview.show();
    BeginScence.addItem(&Gameintroduct);
    Gameintroduct.setPixmap(QPixmap(":/img/introduction.png"));
    Gameintroduct.setPos(QPoint(50,350));
    auto StartButton=new QToolButton();
    StartButton->setAutoRaise(true);
    StartButton->setIcon(QIcon(":/img/start.png"));
    StartButton->setIconSize(QSize(200,50));
    StartButton->move(100,400);
    BeginScence.addWidget(StartButton);
    connect(StartButton,&QToolButton::clicked,[this](){
        mGameview.setSceneRect(QRect(0,0,480,480));

        //场景视图
        mGameview.setScene(&mScene);
        //视图他爹
        mGameview.setParent(this);
        mGameview.show();
        mScene.setSceneRect(QRect(0,0,480,480));
        mBackground.setPixmap(QPixmap(":/img/white.bmp"));
        mCharacter.Mypixmap.setPixmap(QPixmap(":/img/yisa.jpg"));
        //位置
        mCharacter.Mypixmap.setPos(360,170);
        mCharacter.MyPos=QPoint(360,170);
        //添加图片
        mScene.addItem(&mBackground);
        mScene.addItem(&mCharacter.Mypixmap);
        Drawmap();
        CreateEnemy();
    });

//    auto StartBtn1=new QToolButton();
//    StartBtn1->setAutoRaise(true);
//    StartBtn1->setIcon(QIcon(":/new/prefix1/资源/游戏规则.png"));
//    StartBtn1->setIconSize(QSize(200,50));
//    StartBtn1->move(937,695);
//    mGameBeginScence.addWidget(StartBtn1);

//    auto StartBtn2=new QToolButton();
//    StartBtn2->setAutoRaise(true);
//    StartBtn2->setIcon(QIcon(":/new/prefix1/资源/说明返回.png"));
//    StartBtn2->setIconSize(QSize(200,90));
//    StartBtn2->move(0,710);



//    mScene.setSceneRect(QRect(0,0,480,480));
//    mBackground.setPixmap(QPixmap(":/img/white.bmp"));
//    mCharacter.Mypixmap.setPixmap(QPixmap(":/img/yisa.jpg"));
//    //位置
//    mCharacter.Mypixmap.setPos(360,170);
//    mCharacter.MyPos=QPoint(360,170);
//    //添加图片
//    mScene.addItem(&mBackground);
//    mScene.addItem(&mCharacter.Mypixmap);
//    Drawmap();
    //mScene.addItem(&MyCharacter);

    //角色移动检测定时器
    mCharacterMoveTimer=new QTimer(this);
    mCharacterMoveTimer->start(10);
    connect(mCharacterMoveTimer,&QTimer::timeout,this,&MainWindow::Charactermove);
    //子弹发射定时器
    ShootTimer=new QTimer(this);
    ShootTimer->start(mCharacter.mShootSpeed);
    connect(ShootTimer,&QTimer::timeout,this,&MainWindow::MyBulletShoot);
    //子弹移动
    mBulletMoveTimer= new QTimer(this);
    mBulletMoveTimer->start(10);
    connect(mBulletMoveTimer,&QTimer::timeout,[this](){
       for(auto Bullet : mBulletList)
       {
            Bullet->BulletMove();
            //qDebug("2");
            Bulletdis();
            EnemyDie();
       }

    });
    //敌人创建
//    CreateEnemy();
    //敌人移动
    mEnemyMoveTimer=new QTimer(this);
    mEnemyMoveTimer->start(15);
    connect(mEnemyMoveTimer,&QTimer::timeout,[this](){
        for(auto Enemy: mEnemyList)
        {

            Enemy->EnemyMove();

        }
        EnemyDie();
        Gameover();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Charactermove()
{//角色移动
    for(int keyCode:mKeyList)
    {
        switch(keyCode)
        {
            case Qt::Key_W: if(AtWall(1)){  mCharacter.Mypixmap.moveBy(0,-1);}
                            direction=1;  break;
            case Qt::Key_S: if(AtWall(2)){  mCharacter.Mypixmap.moveBy(0,1);}
                            direction=2;  break;
            case Qt::Key_A: if(AtWall(3)){  mCharacter.Mypixmap.moveBy(-1,0);}
                            direction=3;  break;
            case Qt::Key_D: if(AtWall(4)){  mCharacter.Mypixmap.moveBy(1,0);}
                            direction=4;  break;
            default: break;
        }
        Gameover();
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{//按下按键
    switch(event->key())
    {
        case Qt::Key_W:
        case Qt::Key_S:
        case Qt::Key_A:
        case Qt::Key_D:
        mKeyList.append(event->key());
        break;
    }
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{//松开按键
    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());
    }

}

void MainWindow::MyBulletShoot()
{//创建子弹
    QPixmap bulletImg(":/img/bullet.bmp");
    QPoint pos(mCharacter.Mypixmap.x()+10,mCharacter.Mypixmap.y()+10);
    Bullet* bullet=new Bullet(pos,bulletImg,Bullet::BT_Player,direction);
    mScene.addItem(bullet);

    mBulletList.append(bullet);

}
void MainWindow::CreateEnemy()
{//创建敌人
    srand(time(NULL));
    int epos[6];
    for(int i=0;i<3;i++)
    {
        where[i]=qrand()%7;
        if(i==1)
        {
            for(;1;){
                if(where[1]==where[0])
                    where[1]=qrand()%7;
                else
                    break;
            }
        }
        else if(i==2)
        {
            for(;1;){
                if(where[2]==where[0]||where[2]==where[1])
                    where[2]=qrand()%7;
                else
                    break;
            }
        }
        switch(where[i]){//7个预设的出生点
            case 0: epos[i*2]=1;    epos[i*2+1]=1;    break;
            case 1: epos[i*2]=1;    epos[i*2+1]=7;    break;
            case 2: epos[i*2]=4;    epos[i*2+1]=13;    break;
            case 3: epos[i*2]=11;    epos[i*2+1]=17;    break;
            case 4: epos[i*2]=16;    epos[i*2+1]=11;    break;
            case 5: epos[i*2]=18;    epos[i*2+1]=17;    break;
            case 6: epos[i*2]=10;    epos[i*2+1]=2;    break;
        default:    break;
        }
     }
//    switch(qrand()%7){//7个预设的出生点
//        case 0: epos[0]=1;    epos[1]=1;    break;
//        case 1: epos[0]=1;    epos[1]=7;    break;
//        case 2: epos[0]=4;    epos[1]=13;    break;
//        case 3: epos[0]=11;    epos[1]=17;    break;
//        case 4: epos[0]=16;    epos[1]=11;    break;
//        case 5: epos[0]=18;    epos[1]=17;    break;
//        case 6: epos[0]=10;    epos[1]=2;    break;
//    default:    break;
//    }
       QPixmap pixmap(":/img/Goslar.jpg");
//        Enemy* enemy=new Enemy(QPoint(1*20,1*20),pixmap);
//        mScene.addItem(enemy);
//        mEnemyList.append(enemy);
        Enemy* enemy1=new Enemy(QPoint(epos[0]*20,epos[1]*20),pixmap);
        Enemy* enemy2=new Enemy(QPoint(epos[2]*20,epos[3]*20),pixmap);
        Enemy* enemy3=new Enemy(QPoint(epos[4]*20,epos[5]*20),pixmap);
        mScene.addItem(&enemy1->_pixmap);
        mScene.addItem(&enemy2->_pixmap);
        mScene.addItem(&enemy3->_pixmap);
        mEnemyList.append(enemy1);
        mEnemyList.append(enemy2);
        mEnemyList.append(enemy3);

}

void MainWindow::EnemyDie()
{//敌人死亡

    for(int i=0;i<mEnemyList.size();i++)
    {
        for(int j=0;j<mBulletList.size();j++)
        {
            if(mEnemyList[i]->_pixmap.collidesWithItem(mBulletList[j]))
            {
                mScene.removeItem(&mEnemyList[i]->_pixmap);
                mScene.removeItem(mBulletList[j]);
                //qDebug("1");
               // mEnemyList[i]->GameOb
                mEnemyList.removeAt(i);
                mBulletList.removeAt(j);
                rest--;
                if(rest==0)
                {
                    mScene.addItem(&Gamevictory);
                    Gamevictory.setPixmap(QPixmap(":/img/victory .jpg"));
                    Gamevictory.setPos(QPoint(120,120));
                }
            }
        }
    }
}

bool MainWindow::AtWall(int t)
{//角色撞墙判定
    auto* themap=new Chapter1;
    int XX=0,YY=0,bo=0;
    switch(t){
        case 1: XX=0;   YY=-1;    XX=mCharacter.MyPos.x()+XX;   YY=mCharacter.MyPos.y()+YY;
                        if (themap->Map[YY/20][XX/20]&&themap->Map[YY/20][XX/20+1]){bo=1;}
                        break;
        case 2: XX=0;   YY=1;     XX=mCharacter.MyPos.x()+XX;   YY=mCharacter.MyPos.y()+YY;
                        if (themap->Map[YY/20+1][XX/20]&&themap->Map[YY/20+1][XX/20+1]){bo=1;}
                        break;
        case 3: XX=-1;   YY=0;   XX=mCharacter.MyPos.x()+XX;   YY=mCharacter.MyPos.y()+YY;
                        if (themap->Map[YY/20][XX/20]&&themap->Map[YY/20+1][XX/20]){bo=1;}
                        break;
        case 4: XX=1;   YY=0;      XX=mCharacter.MyPos.x()+XX;   YY=mCharacter.MyPos.y()+YY;
                        if (themap->Map[YY/20][XX/20+1]&&themap->Map[YY/20+1][XX/20+1]){bo=1;}
                        break;
    default: break;
    }
    //XX=mCharacter.MyPos.x()+XX;   YY=mCharacter.MyPos.y()+YY;
//    if(mCharacter.MyPos.x()%20)     editx+=1;
//    if(mCharacter.MyPos.y()%20)     edity+=1;
    if(bo)
    {
        mCharacter.MyPos=QPoint(XX,YY);
        return 1;
    }
    else
        return 0;
}

void MainWindow::Bulletdis()
{//子弹撞墙
    for(int i=0;i<mBulletList.size();i++)
    {
        for(int j=0;j<mWallList.size();j++)
        {
            if(mBulletList[i]->collidesWithItem(mWallList[j]))
            {
                mScene.removeItem(mBulletList[i]);
                mBulletList.removeAt(i);
                break;
            }
        }
    }
}

void MainWindow::Gameover()
{//角色去世

    for(int i=0;i<mEnemyList.size();i++)
    {
        if(mEnemyList[i]->_pixmap.collidesWithItem(&mCharacter.Mypixmap))
        {
            mScene.addItem(&Gamedefeat);
            Gamedefeat.setPixmap(QPixmap(":/img/defeat .jpg"));
            Gamedefeat.setPos(QPoint(120,120));
            //qDebug("1");
        }
    }
}

void MainWindow::Drawmap()
{//画地图
    for(int i=0;i<20;i++)
    {
        for(int j=0;j<20;j++)
        {
            QGraphicsPixmapItem *mapitem=new QGraphicsPixmapItem;
            switch (map_1->Map[i][j]){
                case 0:
                    mapitem->setPixmap(QPixmap(":/img/block.jpg"));
                    mapitem->setPos(20*j,20*i);
                    break;
                default:    break;
        }
        mScene.addItem(mapitem);
        mWallList.append(mapitem);
        }
    }
}
